package org.example.util;


import cn.hutool.core.bean.BeanUtil;
import com.deepoove.poi.data.Texts;
import com.deepoove.poi.plugin.table.LoopColumnTableRenderPolicy;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import org.example.annonation.PoiTlSymbol;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class PoiTlUtil {

    public static RenderPolicy loopTablePolicy(int direction) {
        switch (direction) {
            case 1:
                return new LoopRowTableRenderPolicy();
            case 2:
                return new LoopColumnTableRenderPolicy();
        }
        return null;
    }

    public static Map<String, Object> symbolEvent(PoiTlSymbol symbol, Object fieldData) {
        Map<String, Object> result = new HashMap<>();
        if (fieldData == null) {
            return result;
        }
        Map<String, Object> beanToMap = BeanUtil.beanToMap(fieldData);
        for (String key : beanToMap.keySet()) {
            Object value = beanToMap.get(key);
            if (value == null) {
                continue;
            }
            try {
                Boolean flag = (Boolean) value;
                if (flag) {
                    result.put(
                            key, Texts.of(symbol.value()).color(symbol.color()).fontFamily(symbol.fontFamily()).create()
                    );
                } else {
                    result.put(
                            key, Texts.of(symbol.defaultValue()).color(symbol.color()).fontFamily(symbol.fontFamily()).create()
                    );
                }
            } catch (Exception e) {
                continue;
            }
        }
        return result;
    }

    public static Map<String, Object> loopTableEvent(String key, Object fieldData) {
        if (fieldData == null) {
            return new HashMap<>();
        }
        return new HashMap<String, Object>() {{
            put(key, fieldData);
        }};
    }

    public static Map<String, Object> getText(Object fieldData) {
        if (fieldData == null) {
            return new HashMap<>();
        }
        return BeanUtil.beanToMap(fieldData);
    }


}
